//
//  SLStatusRetweetedView.swift
//  Sina15
//
//  Created by 盼 on 16/4/14.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit
import SnapKit

class SLStatusRetweetedView: UIView {
    
    //转发微博的底部约束
    var bottomCons: Constraint?
    
    //绑定数据
    var statusModel: SLStatusViewModel?{
        didSet {
//            print(statusModel)
            contentLabel.text = statusModel?.status?.retweeted_status?.text
            
            let picCount = statusModel?.status?.retweeted_status?.pic_urls?.count ?? 0
    
            //更新约束之前先卸载约束
            self.bottomCons?.uninstall()
            //没有配图
            if picCount == 0 {
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(cellMargin).constraint
                })
                //隐藏配图视图
                pictureView.hidden = true
                
                //给模型数组赋值
                pictureView.pic_URLS = nil
            }else{
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
                })
                //显示配图视图
                pictureView.hidden = false
                
                //设置配图视图的数据源
                pictureView.pic_URLS = statusModel?.status?.retweeted_status?.pic_urls
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    func setupUI() {
        
        //设置灰度
        backgroundColor = UIColor(white:0.9, alpha:1)
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置微博自动计算行高
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(cellMargin)
            make.right.equalTo(self.snp_right).offset(-cellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            make.left.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) -> Void in
          self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
        }
    }
    
    //转发微博正文内容
    private lazy var contentLabel : UILabel = UILabel(labelText: "我是转发微博", color: UIColor.darkGrayColor(), fontsize: 14, margin: cellMargin)
    
    //转发微博配图视图
    private lazy var pictureView: SLStatusPictureView = SLStatusPictureView()

}
